//
//  ButtonCollectionCell.m
//  FKGW
//
//  Created by song ce on 2017/2/27.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "ButtonCollectionCell.h"

@implementation ButtonCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.tagBtn.layer.cornerRadius = 10;
    self.tagBtn.layer.masksToBounds = YES;
}
-(void)setModel:(TagCellModel *)model{
    _model = model;
    [self.tagBtn setTitle:_model.name forState:UIControlStateNormal];
    if(_model.isSelected){
        self.isRefresh = YES;
        [self tagBtn:self.tagBtn];
    }else{
        [self.tagBtn setBackgroundColor:DEF_COLOR242];
        [self.tagBtn setTitleColor:DEF_COLOR102 forState:UIControlStateNormal];
    }
}
- (IBAction)tagBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if(sender.isSelected){
        [self selectedItem];
    }else{
        [self deSelectedItem];
    }
}

//选中
-(void)selectedItem{
    if(self.isSelected&&!self.isRefresh){
        [self deSelectedItem];
        return;
    }
    self.isSelected = YES;
    [self.tagBtn setBackgroundColor:DEF_COLOR];
    [self.tagBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //场景：刷新数据  不需要传递点击事件
    if(self.isRefresh){
        self.isRefresh = NO;
    }else{
        if(self.tagClickedBlock){
            self.tagClickedBlock();
        }
    }
}

//取消选中
-(void)deSelectedItem{
    if(self.isSelected){
        self.isSelected = NO;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"propertySelectedSingle" object:_model];
    }
    self.tagBtn.selected = NO;
    [self.tagBtn setBackgroundColor:DEF_COLOR242];
    [self.tagBtn setTitleColor:DEF_COLOR102 forState:UIControlStateNormal];
}
@end
